package com.resou.reader.reader.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.resou.reader.R;
import com.resou.reader.base.ResouBaseDialog;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 加入书架提示
 * Created by ${Garrett} on 2018/10/23.
 * Contact me krouky@outlook.com
 */
@SuppressLint("ValidFragment")
public class CollectionHintFragment extends ResouBaseDialog {


    private Callback callback;

    public CollectionHintFragment() {
    }
    public CollectionHintFragment(Callback callback) {
        this.callback = callback;
    }

    public void addCallback(Callback callback)
    {
        this.callback = callback;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    private static final String TAG = "CollectionHintFragment";

//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        Window window = getDialog().getWindow();
//        window.requestFeature(Window.FEATURE_NO_TITLE);
//
//        return contentView;
//    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_collection_hint;
    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.cancel_btn, R.id.complete_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_btn:
                if (callback != null) {
                    callback.onOkButtonClicked();
                }
                break;
            case R.id.cancel_btn:
                if (callback != null) {
                    callback.onCancelButtonClicked();
                }
                break;
        }
        dismiss();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
//        window.setBackgroundDrawableResource(R.drawable.bg_login_fragment);
//        WindowManager.LayoutParams attr = window.getAttributes();
//        attr.width = WindowManager.LayoutParams.MATCH_PARENT;
//        attr.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        window.setAttributes(attr);

        Window win = getDialog().getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        if (win != null) {
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        win.setLayout((dm.widthPixels), (int) (dm.heightPixels * 0.5));
            mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams params = win.getAttributes();
            win.setWindowAnimations(R.style.LoginFragmentAnimation);
            params.width = (int) (dm.widthPixels * 0.8);
            win.setAttributes(params);
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        }
    }

    public interface Callback {
        void onOkButtonClicked();

        void onCancelButtonClicked();
    }
}
